MessbereichsCheck <- function(inpath, outpath, para = c("Ta_200", "rH_200", "Ta_10",
                                                     "Ts_5", "Ts_10", "Ts_20",
                                                     "Ts_50", "SM_10", "SM_15", 
                                                     "SM_20", "SM_30", "SM_40", 
                                                     "SM_50", "PAR_300", "Albedo",
                                                     "SWDR_300", "SWUR_300", "WV", 
                                                     "P_container_RT", 
                                                     "P_container_NRT"),digit) {
  
  ##input
  output <- sapply(seq(inpath), function(i) {
        df <- read.table(inpath[i], header = F, sep = ",", 
             fill = T)
  
        qual <- rep("0000000000000000", length(df[,1]))
        qual <- as.character(df[["Qualityflag"]])
  
        qualleft <- substr(qual, 1, (digit - 1))
        qualact <- substr(qual, digit, (digit + 1))
        qualright <- substr(qual, (digit + 2), length(qual))
    
        qualact <- switch(para,
                   "Ta_200" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "rH_200" = ifelse(df[[para]] < 0 || df[[para]] > 100, "r1", "r2"),
                   "Ta_10" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_5" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_10" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_20" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_50" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "SM_10" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_15" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_20" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_30" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_40" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_50" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "PAR_300" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "Albedo" = ifelse(df[[para]] < 0 || df[[para]] > 1, "r1", "r2"),
                   "SWDR_300" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SWUR_300" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "WV" = ifelse(df[[para]] < 0, "r1", "r2"),    
                   "P_container_RT" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "P_container_NRT" = ifelse(df[[para]] < 0, "r1", "r2"))
        
        qual <- paste(qualleft, qualact, qualright, sep = "")

  
        print(qual)
      
        df["Qualityflag"] <- qual
        
  write.table(df, outpath, col.names = T, row.names = F, sep = ",")
  })
}

## EXAMPLE
MessbereichsCheck(
inpath = "C:/Users/Steffi/Documents/=Uni/SpezPhy/Daten/Observator/be_000HEG01_00CEMU_200911010000_200911302330_mez_ca05_cti30_0005.dat",
outpath = "C:/Users/Steffi/Documents/=Uni/SpezPhy/Daten/Out/be_000HEG01_00CEMU_200911010000_200911302330_mez_ca05_cti30_0010.dat",
para = "Ta_200",
digit = 3)
